﻿/******************************************************************************
 *      Class:      EnumConverter
 *      Authors:    Michael Slezak, Samuel Harrington
 *      Implements: IValueConverter
 *      
 * ----------------------------------------------------------------------------
 * 
 *      Description:
 *          This is a converter used in XAML for converting between indices
 *          and its enumerated representation with the proper enumeration.
 *          
 ******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
//   INCLUDES
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace WeatherStationProgram
{
    public class EnumConverter : IValueConverter
    {
       /***********************************************************************
        *      Function: Convert
        *      Authors:  Michael Slezak, Samuel Harrington
        *      Parameters:
        *          object value - the object to convert
        *          Type targetType - the type to convert to
        *          object parameter - not sure what this is and is not relevant to the function
        *          CultureInfo culture - same as parameter. Irrelevant to the function
        *      Returns:  object
        *      Purpose:  Converts an enumerated value to an integer index.
        * *********************************************************************/
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Enum)
            {
                return (int)value;
            }

            return 0;
        }

        /***********************************************************************
        *      Function: ConvertBack
        *      Authors:  Michael Slezak, Samuel Harrington
        *      Parameters:
        *          object value - the object to convert
        *          Type targetType - the type to convert to
        *          object parameter - not sure what this is and is not relevant to the function
        *          CultureInfo culture - same as parameter. Irrelevant to the function
        *      Returns:  object
        *      Purpose:  Converts an index of an enumeration to an enumerated value.
        * *********************************************************************/
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (targetType.IsEnum)
            {
                string name = Enum.GetName(targetType, value);
                return Enum.Parse(targetType, name);
            }
            
            return null;            
        }
    }
}
